package com.lagou.rabbitmq.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lagou.rabbitmq.service.GoodsService;
import com.lagou.rabbitmq.vo.GoodsVO;
import com.lagou.rabbitmq.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @className: GoodsController
 * @auther: Jerry
 * @date: 2021/3/17 23:32
 * @desctiption: TODO
 * @version: 1.0
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @GetMapping("/list")
    public R list(@RequestParam(value = "page",required = false, defaultValue = "1") Integer pageNo,
                  @RequestParam(value = "limit", required = false, defaultValue = "5") Integer pageSize) {
        Page pageParam = new Page(pageNo, pageSize);
        Page page = goodsService.page(pageParam);
        return R.ok(page);
    }

    @PostMapping("/add")
    public R addGoods(@RequestBody GoodsVO goodsVO) {
        goodsVO.setGmtCreate(new Date());
        goodsVO.setGmtModified(new Date());
        goodsService.save(goodsVO);
        return R.ok();
    }

    @PutMapping("/update")
    public R updateGoods(@RequestBody GoodsVO goodsVO) {
        goodsService.updateById(goodsVO);
        goodsVO.setGmtModified(new Date());
        return R.ok();
    }

    @DeleteMapping("/delete/{goodsId}")
    public R delete(@PathVariable("goodsId") Integer goodsId) {
        goodsService.removeById(goodsId);
        return R.ok();
    }

}
